Trying out world map

Author

Axel

Creating a interactive choropleth world map

library(gapminder)
library(ggplot2)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggthemes)
library(RColorBrewer)
world <- map_data("world") %>%
  filter(region != "Antarctica")

##Search for USA or Russia

Cleaning and processing of data, outputing top 5 rows

library(dplyr)
data <- read.csv("/private/clean.csv")
head(data)
  X                          Country     Fertility.Rate Population Country.Code
1 1                            Niger               6.82  24785.587          NER
2 2                          Somalia 6.3120000000000003  16801.170          SOM
3 3                             Chad 6.2549999999999999  16910.218          TCD
4 4 Democratic Republic of the Congo 6.1559999999999997  94374.379          COD
5 5         Central African Republic 5.9779999999999998   5414.014          CAF
6 6                             Mali 5.9560000000000004  21561.299          MLI
data <- data %>%
  mutate(Fertility = as.numeric(as.character(Fertility.Rate)))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Fertility = as.numeric(as.character(Fertility.Rate))`.
Caused by warning:
! NAs introduced by coercion
# Display the first few rows of the modified data
head(data)
  X                          Country     Fertility.Rate Population Country.Code
1 1                            Niger               6.82  24785.587          NER
2 2                          Somalia 6.3120000000000003  16801.170          SOM
3 3                             Chad 6.2549999999999999  16910.218          TCD
4 4 Democratic Republic of the Congo 6.1559999999999997  94374.379          COD
5 5         Central African Republic 5.9779999999999998   5414.014          CAF
6 6                             Mali 5.9560000000000004  21561.299          MLI
  Fertility
1     6.820
2     6.312
3     6.255
4     6.156
5     5.978
6     5.956
# Set figure dimensions
#| fig.width=7
#| fig.height=5



# Display all Brewer palettes
display.brewer.all()

# Filter Gapminder data and join with world map data
  mapa_animado_3 <- data %>%
  right_join(world, by = c(Country = "region")) %>%
  ggplot(aes(long, lat, group = group, fill = `Fertility`)) +
  geom_polygon(color = "white", size = 0.01) +
  theme_void() +
  scale_fill_distiller(palette = "Spectral", name = "Fertility Rate") + # Use a continuous palette
  labs(title = "Population & Fertility Rate", subtitle = "year: 2022")  +
  theme(
    plot.title = element_text(size = 12, hjust = 0.5),
    plot.subtitle = element_text(size = 10, hjust = 0.5),
    plot.caption = element_text(size = 8, hjust = 1)
  ) +
  coord_fixed(ratio = 1.3)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Convert ggplot object to a ggplotly object
fig_3 <- ggplotly(mapa_animado_3)

fig_3
# Assuming 'world' and 'data' are already loaded and prepared
# Create a hover text column in 'data'
data$hover_text <- paste("Country: ", data$Country, "<br>Fertility Rate: ", data$Fertility)

# Proceed with your plotting
mapa_animado_3 <- data %>%
  right_join(world, by = c(Country = "region")) %>%
  ggplot(aes(x = long, y = lat, group = group, fill = Fertility, text = hover_text)) + # Added 'text' aesthetic for custom hover info
  geom_polygon(color = "white", size = 0.01) +
  theme_void() +
  scale_fill_distiller(palette = "Spectral", name = "Fertility Rate") + # Use a continuous palette
  labs(title = "Population & Fertility Rate", subtitle = "year: 2022")  +
  theme(
    plot.title = element_text(size = 12, hjust = 0.5),
    plot.subtitle = element_text(size = 10, hjust = 0.5),
    plot.caption = element_text(size = 8, hjust = 1)
  ) +
  coord_fixed(ratio = 1.3)

# Convert ggplot object to a ggplotly object with hoverinfo
fig_3 <- ggplotly(mapa_animado_3, tooltip = "text") # Specify 'text' to use the 'text' aesthetic for the tooltip